
//判断类型
export function OmTypeOf(val: any): any {
  let toStr = Object.prototype.toString;
  let res: any = {
    "[object Array]": "array",
    "[object Object]": "object",
    "[object Number]": "obj_number",
    "[object String]": "obj_string",
    "[object Boolean]": "obj_boolean",
    "[object Date]": "date",
    "[object RegExp]": "regexp",
  };
  let type = typeof val;
  if (val === null) {
    return "null";
  } else if (type === "object") {
    let ret = toStr.call(val);
    return res[ret];
  } else {
    return type;
  }
}
//获取query参数
export function getQueryString(data: string): any {
  var reg = new RegExp("(^|&)" + data + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) {
    var url = unescape(r[2]);
    var tok = url.indexOf("/");
    if (tok != -1) {
      return url.substring(0, tok);
    } else {
      return url;
    }
  }
  return null;
}

//路由处理
export function formatRouter(routers: any, fatherName?: String) {
  const view = { render: (c: any) => c("router-view") };

  const newRouters = routers.map((r: any, i: number) => {
    let item: any = {
      path: r.path,
      name: r.name,
      meta: {
        title: r.title,
        icon: r.icon,
        operation: r.operation || [],
      },
      component: () =>
        import(`@/views/${fatherName ? fatherName + "/" : ""}${
          r.name
        }/index.vue`),
    };
    if (r.children && r.children.length) {
      item.children = formatRouter(r.children, r.name);
      item.redirect = r.children[0].path;
      item.component = view;
    }
    return item;
  });

  return newRouters;
}

//防抖
export function dobounce(fn: any, time: number, triggleNow: boolean) {
  var t: any = null;
  var dobounce = function(this: any): void {
    var _self = this,
      args = arguments;
    if (t) {
      clearTimeout(t);
    }
    if (triggleNow) {
      var exec = !t;
      t = setTimeout(() => {
        t = null;
      }, time);

      if (exec) {
        fn.apply(_self, args);
      }
    } else {
      t = setTimeout(() => {
        fn.apply(_self, args);
      }, time);
    }
  };

  return dobounce;
}

//节流
function throttle(fn: any, delay: number) {
  var t: any = null,
    begin = new Date().getTime();
  return function(this: any) {
    var self = this,
      args = arguments,
      cur = new Date().getTime();
    clearTimeout(t);

    if (cur - begin >= delay) {
      fn.apply(self, args);
      begin = cur;
    } else {
      t = setTimeout(() => {
        fn.apply(self, args);
      }, delay + begin - cur);
    }
  };
}

//获取url参数
export function getParam(key: string | number) {
  var url = window.location.href;
  if (url.indexOf("?") !== -1) {
    var cookieData = url.split("?")[1].split("&");
    var obj = cookieData.reduce(function(prev: any, item) {
      var arr = item.split("=");
      prev[arr[0]] = arr[1];
      return prev;
    }, {});
    return key ? obj[key] : obj;
  }
}

//深拷贝
export function deepClone(origin: any, target?: any) {
  var target = target || {},
    toStr = Object.prototype.toString,
    arrType = "[object Array]",
    objType = "[object Object]";
  if (toStr.call(origin) !== objType || toStr.call(origin) !== arrType) {
    return origin;
  }
  for (var key in origin) {
    if (origin.hasOwnProperty(key)) {
      if (typeof origin[key] === "object" && origin[key] !== null) {
        if (toStr.call(origin[key]) === arrType) {
          target[key] = [];
        } else {
          target[key] = {};
        }
        target[key] = deepClone(origin[key], target[key]);
      } else {
        target[key] = origin[key];
      }
    }
  }
  return target;
}
//路由对比,暂时解决切换帐号router无效的bug
export function findRouterMeta(path: string, routers: any): any {
  return routers.find((item: any) => {
    if (item.path === path) {
      return item;
    }
    if (item.chilren&&item.children.length) {
      return findRouterMeta(path, item.children);
    }
  });
}
